import static org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


public class JunitTestCases {

	private static PertNetwork pert;
	
	@BeforeClass
	public static void setup(){
		pert = new PertNetwork();
	}
	
	@AfterClass
	public static void tearDown(){
		pert = null;
	}
	
	@Before
	public void clearGraph(){
		pert.clearGraph();
	}
	
	@Test
	public void emptyGraph(){
		assertEquals(true, pert.testGraph().isEmpty());
	}
	
	@Test
	public void oneElement(){
		emptyGraph();
		pert.graph.addVertex("a");
		assertEquals(true, pert.testGraph().get("a").getName() == "a");
	}
	
	@Test 
	public void getNonExistingElement(){
		oneElement();
		assertEquals(true, pert.testGraph().get("b") == null);
	}
	
	@Test
	public void twoElementsNotConnected(){
		getNonExistingElement();
		pert.graph.addVertex("b");
		assertEquals(2, pert.testGraph().size());
		assertEquals(0.0, pert.testGraph().get("a").getEarliestTime(), 0.0010);
		assertEquals(true, pert.testGraph().get("b").getIncomingEdges().isEmpty());
	}
	
	@Test
	public void twoElementsConnected(){
		twoElementsNotConnected();
		pert.graph.putEdge("a", "b", 3);
		assertEquals(0.0, pert.testGraph().get("a").getEarliestTime(), 0.0010);
		assertEquals(0.0, pert.testGraph().get("a").getLastTime(), 0.0010);
		
		assertEquals(3.0, pert.testGraph().get("b").getEarliestTime(), 0.0010);
		assertEquals(3.0, pert.testGraph().get("b").getLastTime(), 0.0010);
		
	}
	
	@Test
	public void assignmentExample(){
		twoElementsConnected();
		
		// recreates the example in the assignment
			pert.graph.addVertex("c");
			pert.graph.addVertex("d");
			pert.graph.addVertex("e");
			pert.graph.addVertex("f");
			pert.graph.addVertex("g");
			
			pert.graph.putEdge("a", "d", 1);
			pert.graph.putEdge("a", "g", 3);
			
			pert.graph.putEdge("b", "c", 1);
			pert.graph.putEdge("b", "f", 1);
			
			pert.graph.putEdge("d", "e", 1);
			
			pert.graph.putEdge("e", "f", 1);
			pert.graph.putEdge("f", "c", 1);
			pert.graph.putEdge("g", "f", 2);
			
			assertEquals(6.0, pert.testGraph().get("c").getEarliestTime(), 0.0010);
			assertEquals(6.0, pert.testGraph().get("c").getLastTime(), 0.0010);
			
			assertEquals(1.0, pert.testGraph().get("d").getEarliestTime(), 0.0010);
			assertEquals(3.0, pert.testGraph().get("d").getLastTime(), 0.0010);
			
			assertEquals(2.0, pert.testGraph().get("e").getEarliestTime(), 0.0010);
			assertEquals(4.0, pert.testGraph().get("e").getLastTime(), 0.0010);
			
			assertEquals(5.0, pert.testGraph().get("f").getEarliestTime(), 0.0010);
			assertEquals(5.0, pert.testGraph().get("f").getLastTime(), 0.0010);
			
			assertEquals(3.0, pert.testGraph().get("g").getEarliestTime(), 0.0010);
			assertEquals(3.0, pert.testGraph().get("g").getLastTime(), 0.0010);
	}
	
	@Test
	public void assignmentExtended(){
		assignmentExample();
		
		pert.graph.addVertex("h");
		pert.graph.addVertex("i");
		pert.graph.addVertex("j");
		pert.graph.addVertex("k");
		pert.graph.addVertex("l");
		
		pert.graph.putEdge("b", "h", 1);
		pert.graph.putEdge("h", "l", 5);
		
		pert.graph.putEdge("g", "i", 1);
		pert.graph.putEdge("g", "f", 2);
		pert.graph.putEdge("i", "j", 1);
		pert.graph.putEdge("j", "k", 4);
		pert.graph.putEdge("f", "k", 2);
		
		pert.graph.putEdge("c", "l", 2);
		pert.graph.putEdge("k", "l", 2);
		
		assertEquals(4.0, pert.testGraph().get("h").getEarliestTime(), 0.0010);
		assertEquals(6.0, pert.testGraph().get("h").getLastTime(), 0.0010);
		
		assertEquals(4.0, pert.testGraph().get("i").getEarliestTime(), 0.0010);
		assertEquals(4.0, pert.testGraph().get("i").getLastTime(), 0.0010);
		
		assertEquals(5.0, pert.testGraph().get("j").getEarliestTime(), 0.0010);
		assertEquals(5.0, pert.testGraph().get("j").getLastTime(), 0.0010);
		
		assertEquals(9.0, pert.testGraph().get("k").getEarliestTime(), 0.0010);
		assertEquals(9.0, pert.testGraph().get("k").getLastTime(), 0.0010);
		
		assertEquals(11.0, pert.testGraph().get("l").getEarliestTime(), 0.0010);
		assertEquals(11.0, pert.testGraph().get("l").getLastTime(), 0.0010);
		
	}

}
